<template>
  <div class="page-content">
    <table-bar :showTop="false" :columns="columns" @changeColumn="changeColumn">
      <div slot="top">
        <el-form
          :model="searchForm"
          :rules="rules"
          label-width="100px"
          ref="searchForm"
        >
          <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :lg="6">
              <form-input
                label="搜索"
                @input="change($event)"
                v-model="searchForm.keyword"
                prop="keyword"
                placeholder="客户名称"
                @keyup.enter.native="submitForm('searchForm')"
              ></form-input>
            </el-col>
            <el-row
              :xs="24"
              :sm="12"
              :lg="6"
              style="float: left;margin-right: 10px;"
            >
              <el-button type="primary" @click="submitForm('searchForm')"
                >搜索</el-button
              >
              <el-button @click="resetForm('searchForm')">重置</el-button>
            </el-row>
          </el-row>
        </el-form>
      </div>
    </table-bar>
    <div class="order-table">
      <k-table
        :data="tableData"
        v-loading="loading"
        ref="shopcarttable"
        @clickHandler="open($event)"
        @sortHandler="sort($event)"
        :page="setpage"
        :total="setpage.total"
        @changePage="gotopage($event)"
        @changeSize="gotosize($event)"
      >
        <el-table-column type="index" width="50" fixed="left">
        </el-table-column>
        <el-table-column label="操作" width="130" fixed="left">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-delete"
              class="el-btn-red"
              @click="deleteClientCart(scope.row)"
            >
              删除
            </el-button>
            <el-button @click="open(scope.row)" icon="el-icon-more" type="text"
              >明细</el-button
            >
          </template>
        </el-table-column>
        <div v-for="(item, index) in tableHeader" :key="index">
          <el-table-column
            :prop="item.name"
            :label="item.title"
            show-overflow-tooltip
            v-if="columns[index].show"
            sortable="custom"
          >
          </el-table-column>
        </div>
      </k-table>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="dialogTableVisible"
      @close="clear"
      width="70%"
    >
      <k-table :data="gridData" v-loading="loading">
        <el-table-column label="操作" width="80">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-delete"
              class="el-btn-red"
              @click="deletePjCart(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          v-for="(item, index) in gridDataHeader"
          :key="index"
          :prop="item.name"
          :label="item.title"
          :width="
            item.name === 'pjcode'
              ? '200'
              : item.name === 'writedate'
              ? '200'
              : ''
          "
        >
        </el-table-column>
      </k-table>
    </el-dialog>
  </div>
</template>

<script>
import { getShopcartApi } from "@/api/orderApi";
import { getShopcartDetailApi } from "@/api/orderApi";
import { DelCartApi } from "@/api/orderApi";
import { DelCartDetailApi } from "@/api/orderApi";
export default {
  inject: ["reload"],
  data() {
    return {
      tableData: [],
      tableHeader: [],
      columns: [],
      tableBody: [],
      gridDataHeader: [],
      loading: true,
      title: "",
      delguestcode: "",
      delddtype: "",
      gridData: [],
      dialogTableVisible: false,
      showEmptyTips: true,
      searchForm: {
        keyword: ""
      },
      setpage: {
        page: 1,
        pageSize: 10,
        total: 0
      },
      rules: {
        keyword: [{ required: true, message: "请输入关键词", trigger: "blur" }]
      }
    };
  },
  mounted() {
    this.CustomerShopCartData("", "", 1);
  },
  methods: {
    CustomerShopCartData(prop, order, pageindex) {
      let userdata = JSON.stringify(this.$store.state.user.user.info);
      userdata = JSON.parse(userdata);
      let guestcode = userdata.reguserno;
      let gscode = userdata.gscode;
      let username = userdata.userName;
      let userid = userdata.id;
      let logintype = userdata.logintype;
      let posttype = userdata.posttype;
      let sort = "";
      if (order != undefined && order.length > 0 && order != null) {
        if (order == "ascending") {
          sort = prop + " asc ";
        } else if (order == "descending") {
          sort = prop + " desc ";
        }
      }
      getShopcartApi({
        reguserno: guestcode,
        guestcode: guestcode,
        gscode: gscode,
        sort: sort,
        pagesize: this.setpage.pageSize,
        pageindex: pageindex,
        username: username,
        userid: userid,
        menuename: "CustomerShopCart",
        menucname: "客户购物车",
        logintype: logintype,
        posttype: posttype,
        op_type: "查询"
      }).then(res => {
        this.loading = false;
        this.tableHeader = [];
        this.tableData = [];
        this.columns = [];
        this.setpage.total = res.count;
        res.filesName.forEach(element => {
          if (element.hidden === false) {
            this.tableHeader.push({ title: element.title, name: element.name });
            this.columns.push({ name: element.title, show: true });
          }
        });
        res.data.forEach(element => {
          this.tableData.push({
            ddamount: element.ddamount,
            ddtype: element.ddtype,
            guestcode: element.guestcode,
            guestname: element.guestname,
            writedate: element.writedate,
            xsprice: parseFloat(element.xsprice).toFixed(2)
          });
        });
      });
    },
    CustomerShopCartdetailData(qpc_guestcode, ywtype) {
      let userdata = JSON.stringify(this.$store.state.user.user.info);
      userdata = JSON.parse(userdata);
      let guestcode = userdata.reguserno;
      let gscode = userdata.gscode;
      getShopcartDetailApi({
        reguserno: guestcode,
        gscode: gscode,
        guestcode: qpc_guestcode,
        ywtype: ywtype
      }).then(res => {
        this.loading = false;
        this.gridDataHeader = [];
        this.gridData = [];
        res.filesName.forEach(element => {
          if (element.hidden === false) {
            this.gridDataHeader.push({
              title: element.title,
              name: element.name
            });
          }
        });
        res.data.forEach(element => {
          this.gridData.push({
            id: element.id,
            writedate: element.writedate,
            pjcode: element.pjcode,
            pjname: element.pjname,
            pjsize: element.pjsize,
            pjfactory: element.pjfactory,
            pjunit: element.pjunit,
            ddamount: element.ddamount,
            xsprice: parseFloat(element.xsprice).toFixed(2),
            note: element.note
          });
        });
      });
    },
    open(row, column, cell, event) {
      this.title = row.guestname;
      this.delguestcode = row.guestcode;
      this.delddtype = row.ddtype;
      this.CustomerShopCartdetailData(row.guestcode, row.ddtype);
      this.dialogTableVisible = true;
    }, // 表格间隔class
    sort(val) {
      this.CustomerShopCartData(val.prop, val.order);
    },
    gotopage(val) {
      this.CustomerShopCartData("", "", val.page);
    },
    gotosize(val) {
      console.log(val);
    },
    clear() {
      this.gridDataHeader = [];
      this.gridData = [];
    },
    deleteClientCart(row) {
      this.$confirm("您确定要清空" + row.guestname + "客户的购物车吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error"
      })
        .then(() => {
          this.handleClick(row);
        })
        .catch(() => {});
    },
    deletePjCart(row) {
      this.$confirm("您确定要删除" + row.pjname + "购物车记录吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error"
      })
        .then(() => {
          this.handleDetailClick(row);
        })
        .catch(() => {});
    },
    handleClick(row) {
      let userdata = JSON.stringify(this.$store.state.user.user.info);
      userdata = JSON.parse(userdata);
      let guestcode = userdata.reguserno;
      let gscode = userdata.gscode;
      let username = userdata.userName;
      let userid = userdata.id;
      let logintype = userdata.logintype;
      let posttype = userdata.posttype;
      DelCartApi({
        guestcode: guestcode,
        gscode: gscode,
        qpc_guestcode: row.guestcode,
        menuename: "CustomerShopCart",
        menucname: "客户购物车",
        username: username,
        userid: userid,
        logintype: logintype,
        posttype: posttype,
        op_type: "删除",
        note: row.guestcode
      }).then(res => {
        this.loading = false;
        if (res.code == "0") {
          this.$message.success("删除成功");
          this.refresh();
        }
      });
    },
    handleDetailClick(row) {
      let userdata = JSON.stringify(this.$store.state.user.user.info);
      userdata = JSON.parse(userdata);
      let guestcode = userdata.reguserno;
      let gscode = userdata.gscode;
      let username = userdata.userName;
      let userid = userdata.id;
      let logintype = userdata.logintype;
      let posttype = userdata.posttype;
      DelCartDetailApi({
        guestcode: guestcode,
        gscode: gscode,
        id: row.id,
        menuename: "CustomerShopCart",
        menucname: "客户购物车",
        username: username,
        userid: userid,
        logintype: logintype,
        posttype: posttype,
        op_type: "删除",
        note: this.delguestcode + "," + row.pjcode
      }).then(res => {
        this.loading = false;
        if (res.code == "0") {
          this.$message.success("删除成功");
          this.CustomerShopCartData("", "", this.setpage.page);
          this.CustomerShopCartdetailData(this.delguestcode, this.delddtype);
        }
      });
    },
    refresh() {
      this.reload();
    },
    changeColumn(columns) {
      this.columns = columns;
      this.doLayout();
    },
    doLayout() {
      this.$nextTick(() => {
        this.$refs.shopcarttable.doLayout();
      });
    },
    change(e) {
      this.$forceUpdate();
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let userdata = JSON.stringify(this.$store.state.user.user.info);
          userdata = JSON.parse(userdata);
          let guestcode = userdata.reguserno;
          let gscode = userdata.gscode;
          getShopcartApi({
            reguserno: guestcode,
            gscode: gscode,
            keyword: this.searchForm.keyword
          }).then(res => {
            this.loading = false;
            this.tableHeader = [];
            this.columns = [];
            this.tableData = [];
            this.setpage.total = res.count;
            res.filesName.forEach(element => {
              if (element.hidden === false) {
                this.tableHeader.push({
                  title: element.title,
                  name: element.name
                });
                this.columns.push({ name: element.title, show: true });
              }
            });
            res.data.forEach(element => {
              this.tableData.push({
                ddamount: element.ddamount,
                ddtype: element.ddtype,
                guestcode: element.guestcode,
                guestname: element.guestname,
                writedate: element.writedate,
                xsprice: parseFloat(element.xsprice).toFixed(2)
              });
            });
          });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang="scss">
.page-content {
  width: 100%;
  height: 100%;
}
.order-table-expand {
  font-size: 0;
}
.order-table-expand label {
  width: 90px;
  color: #2d8cf0;
}
.order-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.order-table {
  height: 100% !important;

  // 分页组件
  ::v-deep .el-pagination {
    margin: 20px 0 0 -5px;
    padding-left: 0;
  }

  // 滚动条
  ::v-deep ::-webkit-scrollbar {
    height: 6px !important;
    display: block !important;
  }

  .empty {
    i {
      color: #d7d6d6;
      font-size: 40px;
    }

    p {
      color: #999999;
      font-size: 13px;
      line-height: 30px;
      margin-top: -10px;
    }
  }
}
.hide-scroll-bar {
  ::v-deep ::-webkit-scrollbar {
    width: 0 !important;
    height: 0 !important;
  }
}
@media only screen and (max-width: $device-notebook) {
}

@media only screen and (max-width: $device-ipad) {
  // .page-content{
  //   display: none;
  // }
}
</style>
